/*
 * =============================================================================
 * Copyright (c) 2008 Exaid. All rights reserved.
 * =============================================================================
 */

/*
 * TransferPanel.java
 *
 * Created on Feb 13, 2009, 8:05:29 AM
 */
package sfsui;

import javax.swing.JProgressBar;
import no.eirikb.sfs.client.LocalShare;
import no.eirikb.sfs.share.ShareFolder;

/**
 *
 * @author eirikb
 */
public class TransferPanel extends javax.swing.JPanel {

    private LocalShare localShare;
    private JProgressBar[] progressBars;
    private int max;
    private int val;

    /** Creates new form TransferPanel */
    public TransferPanel(LocalShare localShare, ShareFolder[] parts) {
        this.localShare = localShare;
        initComponents();
        transferLabel.setText("Downloading " + localShare.getShare().getName());
        progressBars = new JProgressBar[parts.length];
        max = 0;
        val = 0;
        for (int i = 0; i < parts.length; i++) {
            progressBars[i] = new JProgressBar(0, (int) parts[i].getSize());
            max += progressBars[i].getMaximum();
            transferPanel.add(progressBars[i]);
            transferPanel.updateUI();
        }
    }

    public void receiveStatus(int partNumber, long bytes) {
        progressBars[partNumber].setValue(progressBars[partNumber].getValue() + (int) bytes);
        val += bytes;
        int pros = (int) (val * 100L / max);
        transferLabel.setText("Downloading " + localShare.getShare().getName() + "(" + pros + "%)");
        transferPanel.updateUI();
    }

    public void done() {
        transferLabel.setText("Complete " + localShare.getShare().getName());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        transferPanel = new javax.swing.JPanel();
        transferLabel = new javax.swing.JLabel();

        setName("Form"); // NOI18N
        setLayout(new java.awt.BorderLayout());

        transferPanel.setName("transferPanel"); // NOI18N
        transferPanel.setLayout(new java.awt.GridLayout());
        add(transferPanel, java.awt.BorderLayout.CENTER);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(sfsui.SFSUIApp.class).getContext().getResourceMap(TransferPanel.class);
        transferLabel.setText(resourceMap.getString("transferLabel.text")); // NOI18N
        transferLabel.setName("transferLabel"); // NOI18N
        add(transferLabel, java.awt.BorderLayout.PAGE_START);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel transferLabel;
    private javax.swing.JPanel transferPanel;
    // End of variables declaration//GEN-END:variables
}
